﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_admin_ucMain_Hoso : System.Web.UI.UserControl
{
    Bus_Users BUSU = new Bus_Users();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int uid = (int)Session["AdminUid"];
            var dt = BUSU.Users_getAllById(uid);
            rptProfile.DataSource = dt;
            rptProfile.DataBind();
            pnError.Visible = false;
            pnSuccess.Visible = false;
        }
    }

    protected void btnSua_Click(object sender, EventArgs e)
    {
        try 
	    {	
		    DTO_Users user = new DTO_Users();
            TextBox txtName = (TextBox)rptProfile.Items[0].FindControl("txtName");
            TextBox txtEmail = (TextBox)rptProfile.Items[0].FindControl("txtEmail");
            if (txtName.Text != "" && txtEmail.Text != "")
            {
                user.Uid = (int)Session["AdminUid"];
                user.Name = txtName.Text.Trim();
                user.Email = txtEmail.Text.Trim();
                user.Status = 1;
                var dt = BUSU.Users_getAllByEmail(user.Email);
                if (dt.Rows.Count > 0 && user.Email != Session["AdminEmail"].ToString())
                {
                    pnSuccess.Visible = false;
                    pnError.Visible = true;
                    lblError.Text = "Email này đã được sử dụng. Xin chọn email khác.";
                }
                else
                {
                    Session["AdminName"] = user.Name;
                    Session["AdminEmail"] = user.Email;
                    BUSU.Users_Update(user);
                    pnError.Visible = false;
                    pnSuccess.Visible = true;

                }
            }
            else
            {
                pnSuccess.Visible = false;
                pnError.Visible = true;
                lblError.Text = "Xin vui lòng điền đầy đủ thông tin bên dưới.";
            }

	    }
	    catch
	    {
	    }
        
    }
}